package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateTables {

	/**
	 * @param args
	 */
	public static Boolean Create(String path) {

			
			try {
			
				Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

                Connection conn = DriverManager.getConnection("jdbc:derby:"+path+";create=true");

                Statement command = conn.createStatement();
                
                
				// Crates bird table. All items are strings. Although slower it is WAY easier to manage.
				try {
					command.executeUpdate(
							"CREATE TABLE bird (scientific_name VARCHAR(1000) NOT NULL, "+
							"risk_of_extinction varchar(1000), "+
							"imageurl varchar(1000), "+
							"weight decimal, "+
							"habitat varchar(1000), "+
							"incubation_time decimal, "+
							"longevity decimal, "+
							"color varchar(5000), "+
							"size decimal, "+
							"clutchsize decimal, "+
							"diet varchar(1000), "+
							"PRIMARY KEY (scientific_name))");
					
					System.out.println("Table Bird created succesfully");
				}
					catch (SQLException err){
						System.out.println("Can't create table 'bird': " + err.getMessage());
					}
					
				//Creates country table
				
				try {
					command.executeUpdate(
							"CREATE TABLE country (bird_scientific_name varchar(1000) NOT NULL, "+
							"country_name VARCHAR(1000) NOT NULL, "+
							"PRIMARY KEY (bird_scientific_name, country_name), " +
							"foreign key (bird_scientific_name) references bird(scientific_name))");
					
					System.out.println("Table Country created succesfully");
				}
					catch (SQLException err){
						System.out.println("Can't create table 'country': " + err.getMessage());
					}
				
				//Creates popular name table
				try {
					command.executeUpdate(
							"CREATE TABLE popular_names (name VARCHAR(1000) NOT NULL, "+
							"bird_scientific_name varchar(1000) NOT NULL, "+
							"PRIMARY KEY (name, bird_scientific_name), " +
							"foreign key (bird_scientific_name) references bird(scientific_name))");
					
					System.out.println("Table Popular Name created succesfully");
				}
					catch (SQLException err){
						System.out.println("Can't create table popular name: " + err.getMessage());
					}
				
				//tries to close the db connection
				try {
					command.close();
					conn.close();
				} catch (SQLException e)
				
				{
					System.out.println("Error on close: "+e);
					e.printStackTrace();
				}
				
				}catch(ClassNotFoundException errclass){
	                System.out.println("Missing class! Check your CLASSPATH");
				
				} catch (SQLException e) {
					System.out.println("Error: "+e);
					e.printStackTrace();
				}
			
	

		System.out.println("Tables successfully created.");
		return true;
	}
}